
#include "XBeeMessage.h"
#include "XBeeSerial.h"

XBeeMessage::XBeeMessage() {
    apiID = 0;
    frameID = 0;
    use16BitAddress = false;
    use64BitAddress = false;
    address16 = 0;
    address64 = 0;
    msgDataLength = 0;
    rawDataLength = 0;
    msgData = 0;
    rawData = 0;
}

XBeeMessage::~XBeeMessage() {
    if(msgData != 0)
        delete msgData;
    if(rawData != 0)
        delete rawData;
}

void XBeeMessage::set16BitAddress(unsigned short addr) {
    use16BitAddress = true;
    address16 = addr;
}

void XBeeMessage::set64BitAddress(unsigned long addr) {
    use64BitAddress = true;
    address64 = addr;
}

int XBeeMessage::getMessageSum() {
    int total = apiID + frameID;

    if (use64BitAddress)
    {
        char* addr = int64ToBytes(address64);
        for(int i=0; i < 8; i++)
            total += addr[i];
        delete addr;
    }

    if (use16BitAddress)
    {
        char* addr = int16ToBytes(address16);
        for(int i=0; i < 2; i++)
            total += addr[i];
        delete addr;
    }

    for(int i = 0; i < msgDataLength; i++)
        total += msgData[i];
        
    return total;
}
